<?php

namespace app\admin\controller;

use app\admin\validate\AdminUser;
use app\common\business\SysUser;
use app\common\business\SysLogininfor;

class Login extends ApiBase
{

    public function check(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验 1.原生方式 2.TP6验证机制
        $uuid = $this->request->param("uuid","","trim");
        $username = $this->request->param("username","","trim");
        $password = $this->request->param("password","","trim");
        $captcha = $this->request->param("captcha","","trim");
        $data = [
            'username'=>$username,
            'password'=>$password,
            'captcha'=>$captcha,
            'uuid'=>$uuid,
        ];
        $validate = new AdminUser();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        /*if(empty($username) || empty($password) || empty($captcha)){
            return show(config("status.error"),"参数不能为空");
        }*/
        $sysLogininfor = (new SysLogininfor());
        //需要校验验证码
        if(!captcha_check($captcha,$uuid)){
            //验证码校验失败
            $sysLogininfor->addLogininfor('验证码不正确',$username,1);
            return show(config("status.error"),"验证码不正确");
        }
        try {
            $SysUser = new SysUser();
            $result = $SysUser->login($data);
        }catch (\Exception $e){
            return show(config("status.error"),$e->getMessage());
        }
        if($result){
            $sysLogininfor->addLogininfor('登录成功',$username);
            return show(config("status.success"),"登录成功",$result);
        }else{
            return show(config("status.error"),"登录失败");
        }
    }
}
